import {createRouter, createWebHistory} from "vue-router";

import WelcomeView from "@/components/views/WelcomeView.vue";
import {isUnAuhtorized} from "@/net/index.js";
const routes = [
    { path: '/'
        ,name:'welcome',
        component: WelcomeView,
        children:[
            {
                path: '',
                name: 'welcome-login',
                component: () => import('@/components/views/welcome/LoginPage.vue')
            },
            {
                path: 'register',
                name: 'welcome-register',
                component: () => import('@/components/views/welcome/RegisterPage.vue')
            },
            {
                path: 'forget',
                name: 'welcome-forget',
                component: () => import('@/components/views/welcome/ForgetPage.vue')
            },

        ]},
    {
        path:"/index",
        name:"index",
        component:()=>import("@/components/views/IndexView.vue"),
        children: [
            {
              path: '',
              name: 'topics',
              component: () => import('@/components/views/forum/Fourm.vue'),
              children: [            {
                  path: '',
                  name: 'topic-list',
                  component: () => import('@/components/views/forum/TopicList.vue')

              },
                  {
                      path: 'topic-detail/:tid',
                      name: 'topic-detail',
                      component: () => import('@/components/views/forum/TopicDetail.vue')
                  }

              ]
            },
            {
                path: 'user-setting',
                name: 'user-setting',
                component: () => import('@/components/views/settings/UserSetting.vue')
            },
            {
                path: 'privacy-setting',
                name: 'privacy-setting',
                component: () => import('@/components/views/settings/PrivacySetting.vue')

            },

        ]
    }
]

const router = createRouter ({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes,
})
router.beforeEach((to, from, next) => {
        if (to.name.startsWith('welcome' )&& !isUnAuhtorized()) {
            next('/index')
        } else if (to.fullPath.startsWith('/index') && isUnAuhtorized()) {
            next('/')
        }else {
            next()
        }
    }
)
export default router